<template>
  <div class="app-container home">
    <div class="bg_fff mb_12 pl_14">
      <breadcrumb />
    </div>
    <div class="bg_fff p_14 pb_0 mb_12">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true">
        <el-form-item label="用户名：" prop="name">
          <el-input
            v-model="queryParams.name"
            placeholder="请输入用户名"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="会员ID：" prop="id">
          <el-input
            v-model="queryParams.id"
            placeholder="请输入会员ID"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="queryParams.status" placeholder="请选择用户状态" clearable @change="handleQuery">
            <el-option
              v-for="dict in statusList"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="资质审核状态" prop="check_status">
          <el-select v-model="queryParams.check_status" placeholder="请选择资质审核状态" clearable @change="handleQuery">
            <el-option
              v-for="(dict,val) in zzList"
              :key="dict"
              :label="dict"
              :value="val"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="bg_fff p_10 listbody">
      <div class="flexrowbetween flexwrapper mb_15" style="align-items: flex-start">
        <div class="flex">
        </div>
        <div>
           <el-button plain type="primary" size="mini" @click="handleExport"  v-hasPermi="['usermgt:export']">导出</el-button>
        </div>
      </div>
      <el-table
        v-loading="loading"
        :data="tableData"
        ref="tablelayout"
        class="commtable"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column label="序号" type="index" align="center">
          <template slot-scope="scope">
            <span>{{(queryParams.page - 1) * queryParams.num + scope.$index + 1}}</span>
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          label="ID"
          prop="id"
          width="120"
          :show-overflow-tooltip="true"
        ></el-table-column>
        <el-table-column
          align="center"
          label="用户名"
          min-width="160"
          prop="user_login"
          :show-overflow-tooltip="true"
        ></el-table-column>
        <el-table-column
          align="center"
          label="手机号"
          prop="mobile"
          min-width="160"
          :show-overflow-tooltip="true"
        ></el-table-column>
        <el-table-column
          align="center"
          label="广告币余额"
          prop="mobile"
          min-width="120"
          :show-overflow-tooltip="true"
        >
          <template slot-scope="scope">
            <span>{{ scope.row.score - scope.row.freeze_score || '-'}}</span>
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          label="邮箱"
          prop="email"
          min-width="120"
          :show-overflow-tooltip="true"
        >
          <template slot-scope="scope">
            <span>{{scope.row.email || '-'}}</span>
          </template>
        </el-table-column>
        <!-- <el-table-column
          align="center"
          label="开通时间"
          prop="role_user.role.name"
        >
          <template slot-scope="scope">
            <span>{{scope.row.create_time || '-'}}</span>
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          label="有效期"
          prop="role_user.role.name"
        >
          <template slot-scope="scope">
            <span v-if="scope.row.role_user">{{scope.row.role_user.role.name || '-'}}</span>
            <span v-else>-</span>
          </template>
        </el-table-column> -->
        <el-table-column
          align="center"
          label="登录时间"
          prop="last_login_time"
          min-width="200"
          :show-overflow-tooltip="true"
        >
          <template slot-scope="scope">
            {{scope.row.last_login_time || '-'}}
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          label="注册时间"
          min-width="200"
          prop="create_time"
          :show-overflow-tooltip="true"
        >
          <template slot-scope="scope">
            {{scope.row.create_time || '-'}}
          </template>
        </el-table-column>
        <el-table-column label="审核状态" align="center" min-width="160" :show-overflow-tooltip="true">
          <template slot-scope="scope">
            <span>{{scope.row.check_status_name}}</span>
            <!-- <el-tag v-if="scope.row.check_status == 1" type="success" class="tag_success">{{scope.row.check_status_name}}</el-tag>
            <el-tag v-else-if="scope.row.check_status == 4" type="warning" class="tag_warning">{{scope.row.check_status_name}}</el-tag>
            <el-tag v-else type="danger" class="tag_danger">{{scope.row.check_status_name}}</el-tag> -->
          </template>
        </el-table-column>
        <el-table-column label="状态" align="center" min-width="120" :show-overflow-tooltip="true">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.status == 1" type="success" class="tag_success">正常</el-tag>
            <el-tag v-else type="danger" class="tag_danger">暂停</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="广告计划总数量" align="center"  min-width="160" :show-overflow-tooltip="true">
          <template slot-scope="scope">
            <span>{{scope.row.data.count||0}}</span>
          </template>
        </el-table-column>
        <el-table-column label="投放中数量" align="center" min-width="120" :show-overflow-tooltip="true">
          <template slot-scope="scope">
            <span>{{scope.row.data.work_count||0}}</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="created_at"
          label="操作"
          width="200"
          fixed="right"
          align="center"
        >
          <template slot-scope="scope">
            <div class="flexcenter btnflex">
              <div class="commhover mr_10"
                  @click="handleGround(scope.row)" v-hasPermi="['usermgt:gotouser']">
                  <el-tooltip class="item" effect="dark" content="前台" placement="top-start">
                    <svg-icon class-name="f-24 c_primary" icon-class="qiantai" />
                  </el-tooltip>
              </div>
              <div class="commhover mr_10"
                  @click="handleChong(scope.row)" v-hasPermi="['usermgt:recharge']">
                  <el-tooltip class="item" effect="dark" content="充值" placement="top-start">
                    <svg-icon class-name="f-24 c_FF9D00" icon-class="chongzhi" />
                  </el-tooltip>
              </div>
              <div class="commhover mr_10"
                  @click="handleUpdate(scope.row)" v-hasPermi="['usermgt:edit']">
                  <el-tooltip class="item" effect="dark" content="编辑" placement="top-start">
                    <i class="el-icon-edit-outline f-24 c_primary"></i>
                  </el-tooltip>
              </div>
              <div class="commhover mr_10" v-if="scope.row.check_status==3"
                  @click="handleShenghe(scope.row)" v-hasPermi="['usermgt:shenhe']">
                  <el-tooltip class="item" effect="dark" content="审核" placement="top-start">
                    <svg-icon class-name="f-24 c_FF9D00" icon-class="check" />
                  </el-tooltip>
              </div>
              <!-- <div class="commhover mr_10"
                  @click="handleTui(scope.row)">
                  <el-tooltip class="item" effect="dark" content="退款" placement="top-start">
                    <svg-icon class-name="f-24 c_FF9D00" icon-class="tui" />
                  </el-tooltip>
              </div> -->
            </div>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.page"
        :limit.sync="queryParams.num"
        @pagination="getList"
      />
    </div>
    <el-dialog
      title="编辑"
      :visible.sync="open"
      width="680px"
      class="commdia"
      :before-close="handleClose"
      :close-on-click-modal='false'
    >
      <edit-form v-if="open" :rowitem='rowitem' @close='handleClose' />
    </el-dialog>

    <el-dialog
      title="充值"
      :visible.sync="c_open"
      width="680px"
      class="commdia"
      :before-close="handleCloseCz"
      :close-on-click-modal='false'
    >
      <chong-form v-if="c_open" :rowitem='rowitem' @close='handleCloseCz' />
    </el-dialog>

    <el-dialog
      title="审核"
      :visible.sync="opensh"
      width="860px"
      class="commdia bgfa"
      :before-close="handleCloseSh"
      :close-on-click-modal='false'
    >
      <sheng-he v-if="opensh" :rowitem='rowitem' @close='handleCloseSh' />
    </el-dialog>

    <el-dialog
      title="退款"
      :visible.sync="opentui"
      width="680px"
      class="commdia"
      :before-close="handleCloseTui"
      :close-on-click-modal='false'
    >
      <tui-form v-if="opentui" :rowitem='rowitem' @close='handleCloseTui' />
    </el-dialog>
  </div>
</template>

<script>
import { mapState } from "vuex";
import EditForm from './components/EditForm.vue'
import ChongForm from './components/ChongForm.vue'
import ShengHe from './components/ShengHe.vue'
import TuiForm from './components/TuiForm.vue'
import { getAdvmsterList,userDel,openStatus, getCustomToken,reqCustomIndexExport } from '@/api/advmaster'
import { getToken } from '@/utils/auth'
export default {
  data() {
    return {
      statusList:[{value:1,label:'正常'},{value:0,label:'暂停'}],
      zzList:[],
      queryParams: {
        page: 1,
        num: 10,
        name:undefined,
        id:undefined,
        check_status:undefined,
        status: undefined
      },
      total: 1,
      loading: false,
      tableData: [],
      checkedOption: [],
      exportColumns: [
        {key: 'id', label: `ID`,},
        {key: 'user_login', label: `用户名`},
        {key: 'mobile', label: `手机号`,},
        {key: 'score(-)freeze_score', label: `广告币余额`},
        {key: 'email', label: `邮箱`,},
        {key: 'last_login_time', label: `登录时间`,},
        {key: 'create_time', label: `注册时间`,},
        {key: 'check_status_name', label: `审核状态`,},
        {key: 'status_name', label: `状态`,},
        {key: 'data.count', label: `广告计划总数量`,},
        {key: 'data.work_count', label: `投放中数量`,},
      ],
      paramsForm:{},
      rolesList:[],
      rowitem:null,
      multiple: true,
      title: "",
      // 是否显示弹出层
      open: false,
      c_open: false,
      opensh:false,
      opentui:false
    };
  },

  props: {
    type: {
      type: Number,
      default: 1,
    },
  },

  components:{
    EditForm,
    ChongForm,
    ShengHe,
    TuiForm
  },

  computed: {
    ...mapState({
      adminOption: (state) => state.paramsoption.adminOption,
    }),
  },

  created() {
    let adminOption = JSON.parse(localStorage.getItem("adminOption"));
    if (adminOption) {
      this.changeSelect(adminOption);
    }
    this.getDicts('/manager/api/getCustomCheckStatus').then(res=>{
      this.zzList = res.data
    })
    this.checkedOption = this.adminOption;
    this.getList()
  },

  methods: {
    handleExport() {
      let form = JSON.parse(JSON.stringify(this.paramsForm))
      let head = []
      let keys = [];
      console.log('checkedOption',this.checkedOption);
      this.exportColumns.forEach(item => {
            if (head.indexOf(item.label) == -1) {
              head.push(item.label)
            }
            if (keys.indexOf(item.key) == -1) {
              keys.push(item.key)
            }


      })
      form.head = head
      form.keys = keys
      delete form.page
      delete form.num
      reqCustomIndexExport(form).then((res) => {
        console.log('333', res)
        if (res.code == 1) {
          const a = document.createElement('a')
          document.body.appendChild(a)
          a.download = '广告主列表数据.xls'
          a.href = res.data
          a.click()
          document.body.removeChild(a)
        }

      })

    },
    getList() {
      this.loading = true;
      let params = {
        page: this.queryParams.page,
        num: this.queryParams.num
      }
      if(this.queryParams.name){
        params.name = this.queryParams.name
      }
      if(this.queryParams.id){
        params.id = this.queryParams.id
      }
      if(this.queryParams.status||this.queryParams.status==0){
        params.status = this.queryParams.status
      }
      if(this.queryParams.check_status||this.queryParams.check_status==0){
        params.check_status = this.queryParams.check_status
      }
      this.paramsForm=params;
      getAdvmsterList(params).then((response) => {
        this.tableData = response.data.list;
        this.total = response.data.count;
        this.loading = false;
      });
      this.loading = false;
    },

    changeSelect(arr) {
      this.$store.commit("SET_ADMINOPTION", arr);
      this.$nextTick(() => {
        //在数据加载完，重新渲染表格
        this.$refs.tablelayout && this.$refs.tablelayout.doLayout();
      });
    },

    handleQuery() {
      this.queryParams.page = 1;
      this.getList();
    },

    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    handleSelectionChange(selection) {
      this.rowids = selection.map((item) => item.id);
      this.multiple = !selection.length;
    },

    reset() {
      this.form = {
        id:undefined,
        role_id: undefined,
        name: undefined,
        nickname:undefined,
        email:undefined,
        password:undefined,
        phone:undefined,
        status: 1
      };
      this.resetForm("form");
    },

    getRoles(){
      this.getDicts('/manager/api/getRoleList').then(res=>{
        this.rolesList = res.data
      })
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.getRoles()
      this.open = true;
      this.title = "添加";
    },

    handleClose(type) {
      if(type == 1){
        this.getList()
      }
      this.open = false;
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.rowitem = row
      this.open = true;
      this.title = "修改";
    },

    handleChong(row){
      this.rowitem = row
      this.c_open = true;
    },

    handleCloseCz(type){
      if(type == 1){
        this.getList()
      }
      this.c_open = false;
    },

    handleShenghe(row){
      this.rowitem = row
      this.opensh = true;
    },

    handleCloseSh(type){
      if(type == 1){
        this.getList()
      }
      this.opensh = false;
    },

    handleTui(row){
      this.rowitem = row
      this.opentui = true;
    },

    handleCloseTui(type){
      if(type == 1){
        this.getList()
      }
      this.opentui = false;
    },

    /** 暂停按钮操作 */
    handleDelete(row) {
      const infoIds = row.id;
      this.$modal
        .confirm('是否确认暂停编号为"' + infoIds + '"的用户？')
        .then(function () {
          return userDel({id:infoIds});
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("暂停成功");
        })
        .catch(() => {});
    },

    handleStart(row) {
      const infoIds = row.id;
      this.$modal
        .confirm('是否确认启用编号为"' + infoIds + '"的用户？')
        .then(function () {
          return openStatus({id:infoIds});
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("启用成功");
        })
        .catch(() => {});
    },


    handleGround(row){
      getCustomToken({id:row.id}).then(res=>{
        let token = res.data;
        //  + "&activeId=" + row.id
        console.log('getToken----',getToken());
        window.open(
          this.userdomain + 'login?token=' + token + '&adminToken=' + getToken(),
        );
      })
    }
  },
};
</script>

<style lang="scss" scoped>
.iconbox {
  width: 32px;
  height: 32px;
  border: 1px solid #eee;
  border-radius: 2px;
}
</style>
